﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace FortuneHeaven.Admin.Controls
{
    public partial class TxtPagination : System.Web.UI.UserControl
    {
        private int size =5;
        public delegate void PagerChangeHandler(object sender, int currentpage);
        public event PagerChangeHandler PagerChanged;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                
            }
        }

        private void SetPages()
        {
            lbTotalPage.Text = PageCount.ToString();
        }

        public void Initial()
        {
            SetPages();
        }

        public void EnableBtn(LinkButton btn)
        {
            btn.Enabled = true;
            btn.CssClass = "";
        }
        public void DisableBtn(LinkButton btn)
        {
            btn.Enabled = false;
            btn.CssClass = "disbtn";
        }

        private void SetButtonStatus()
        {
            DisableBtn(btnPrev);
            DisableBtn(btnNext);
            if (PageCount > 1)
            {
                if(CurrentPageIndex>1)
                {
                    EnableBtn(btnFirst);
                    EnableBtn(btnPrev);
                }
                else{
                    DisableBtn(btnFirst);
                    DisableBtn(btnPrev);
                }
                if (CurrentPageIndex < PageCount)
                {
                    EnableBtn(btnNext);
                    EnableBtn(btnFinal);
                }
                else
                {
                    DisableBtn(btnNext);
                    DisableBtn(btnFinal);
                }
            }
            else
            {
                DisableBtn(btnFirst);
                DisableBtn(btnFinal);
                DisableBtn(btnPrev);
                DisableBtn(btnNext);
            }
        }
	
        public int CurrentPageIndex
        {
            get
            {
                object cpage = ViewState["CurrentPageIndex"];
                int pindex = (cpage == null) ? 1 : (int)cpage;
                if (pindex > PageCount && PageCount > 0)
                    return PageCount;
                else if (pindex < 1)
                    return 1;
                return pindex;
            }
            set
            {
                int cpage = value;
                if (cpage < 1)
                    cpage = 1;
                else if (cpage > this.PageCount)
                    cpage = this.PageCount;
                ViewState["CurrentPageIndex"] = cpage;
                txtToPage.Text = cpage.ToString();
                SetButtonStatus();
                RaiseEvent();
            }
        }

        public int RecordCount
        {
            get
            {
                object obj = ViewState["Recordcount"];
                return (obj == null) ? 0 : (int)obj;
            }
            set
            {
                ViewState["Recordcount"] = value;
            }
        }
	
        public int PagesRemain
        {
            get
            {
                return PageCount - CurrentPageIndex;
            }
        }	

        public int PageSize
        {
            get
            {
                object obj = ViewState["PageSize"];
                if (obj == null)
                {
                    obj = size;
                }
                return (obj == null) ? size : (int)obj;
            }
            set
            {
                int pageSize = value;

                if (Math.Abs(pageSize) == 0)
                    pageSize = size;

                ViewState["PageSize"] = pageSize;
            }
        }

        public int RecordsRemain
        {
            get
            {
                if (CurrentPageIndex < PageCount)
                {
                    return RecordCount - (CurrentPageIndex * PageSize);
                }
                else
                {
                    return 0;
                }
            }
        }

        public int PageCount
        {
            get { return (RecordCount > 0) ? (int)Math.Ceiling((double)RecordCount / (double)PageSize) : 1; }
        }

        protected void btnFirst_Click(object sender, EventArgs e)
        {
            CurrentPageIndex = 1;
        }

        protected void btnPrev_Click(object sender, EventArgs e)
        {
            CurrentPageIndex = (CurrentPageIndex > 1) ? CurrentPageIndex - 1 : 1;
        }

        protected void btnNext_Click(object sender, EventArgs e)
        {
            CurrentPageIndex = (PageCount > CurrentPageIndex) ? CurrentPageIndex + 1 : PageCount;
         
        }

        protected void btnFinal_Click(object sender, EventArgs e)
        {
            CurrentPageIndex = PageCount;
        }


        public void RaiseEvent()
        {
            if (PagerChanged != null)
            {
                PagerChanged(this, CurrentPageIndex);
            }
        }

        protected void btnGotoClick(object sender, EventArgs e)
        {
            int ret = 1;
            int.TryParse(this.txtToPage.Text, out ret);
            if (ret < 1)
                this.txtToPage.Text = "1";
            else if (ret > this.PageCount)
                this.txtToPage.Text = this.PageCount.ToString();
            CurrentPageIndex = ret;
        }

    }
}